<template>
  <div class="menu-container">
    <div class="menu-top">
      <div class="menu-search"></div>
      <div class="menu-btn"><el-button type="primary" size="small" @click="isShowDialog = true">新增</el-button></div>
    </div>
    <el-scrollbar style="height: calc(100vh - 160px)" class="scrollbar" id="scroll-bar">
      <el-table :data="list">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-image
              v-for="item in props.row.banner_list"
              :key="item.data_id"
              style="width: 100px; height: 60px"
              fil="cover"
              :src="getImageUrl('banner', item.data_id, 'url')"
              :preview-src-list="[getImageUrl('banner', item.data_id, 'url')]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="菜单名称" prop="menu_name"></el-table-column>
        <el-table-column label="最后修改时间" width="160" prop="data_update_time"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="updBanner(scope.row.data_id, scope.row.menu_name)" size="mini">
              修改
            </el-button>
            <el-button type="danger" @click="delBanner(scope.row.data_id)" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-scrollbar>
    <paging ref="page" :func="getList"></paging>

    <el-dialog @closed="onClose" title="提示" :visible.sync="isShowDialog">
      <el-form :model="params" :rules="rules" ref="dataParams" label-width="120px" class="demo-dataParams">
        <el-form-item label="菜单名称" prop="menu_name">
          <el-input v-model="params.menu_name" placeholder="请输入菜单名称"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowDialog = false">取 消</el-button>
        <el-button type="primary" @click="dialogConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import paging from '@/components/paging';
  import menuManage from '@/api/menu';
  import { getImageUrl } from '@/utils/image';
  export default {
    name: 'menu-view',
    components: {
      paging,
    },
    data() {
      return {
        list: [],
        isShowDialog: false,
        params: {
          menu_name: '',
        },
        menu_id: '',
        rules: {
          menu_name: [{ required: true, message: '请输入菜单名称', trigger: 'blur' }],
        },
      };
    },
    async mounted() {
      this.refresh();
    },
    methods: {
      getImageUrl,
      refresh() {
        this.$refs.page.refresh();
      },
      search() {
        this.$refs.page.search();
      },
      async getList(callback) {
        let res = await menuManage.getMenu();
        this.list = res.item_list;
        callback(this.list, res.total_count);
      },
      updBanner(data_id, name) {
        this.updId = data_id;
        this.params.menu_name = name;
        this.isShowDialog = true;
      },
      async delBanner(data_id) {
        this.$alert('是否确定删除菜单', '删除', {
          confirmButtonText: '确定',
          callback: async (action) => {
            if (action === 'confirm') {
              await menuManage.delMenu(data_id);
              this.getList();
            }
          },
        });
      },
      async dialogConfirm() {
        this.$refs.dataParams.validate(async (valid) => {
          if (valid) {
            let res = null;
            if (this.updId) {
              res = await menuManage.updMenu(this.updId, this.params);
            } else {
              res = await menuManage.setMenu(this.params);
            }
            if (res) {
              this.isShowDialog = false;
              this.getList();
            }
          }
        });
      },
      onClose() {
        this.updId = '';
        this.params = {
          menu_name: '',
        };
      },
    },
  };
</script>

<style lang="less" scoped>
  .menu-container {
    height: 100%;
    .menu-top {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 12px;
      .menu-search {
        display: flex;
      }
    }
    .menu-list {
      display: flex;
      flex-flow: row wrap;
      width: 100%;
      height: 100%;
      .menu-item {
        width: calc(50% - 6px);
        height: 100px;
        display: flex;
        margin-bottom: 6px;
        border: 1px solid #f3f3f3;
        .item-content {
          margin: 10px;
          p {
            line-height: 40px;
          }
        }
      }
    }
  }
</style>
